package com.hsy.crm.web.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hsy.crm.web.domain.cm.App;
import com.hsy.crm.web.domain.cm.PageResult;
import com.hsy.crm.web.domain.cm.Unified;
import com.hsy.crm.web.domain.entity.StoreProduct;
import com.hsy.crm.web.domain.ex.ServiceException;
import com.hsy.crm.web.domain.po.stoneProduct.StoneProductPagePo;
import com.hsy.crm.web.domain.po.stoneProduct.StoneProductPo;
import com.hsy.crm.web.domain.vo.stoneProduct.StoneProductPageVo;
import com.hsy.crm.web.mapper.StoreProductMapper;
import com.hsy.crm.web.service.base.BaseService;
import com.hsy.crm.web.utils.StringUtils;
import com.hsy.crm.web.utils.UuidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StoreProductService extends BaseService<StoreProduct> {

    @Autowired
    StoreProductMapper storeProductMapper;

    public PageResult<StoneProductPagePo> page(StoneProductPageVo pageVo) {
        PageResult<StoneProductPagePo> pageResult = new PageResult<>();
        pageResult.setRows(storeProductMapper.page(pageVo));
        return pageResult;
    }

    /**
     * 更新库存 -- 主库存
     */
    public void update(StoreProduct storeProduct, int direction){
        check(storeProduct);
        Long storeProductId = storeProductMapper.selectStoreProduct(storeProduct);
        if( storeProductId == null ){
            if( direction == App.StoreType.OUT ){
                storeProduct.setNumber(-storeProduct.getNumber());
                storeProduct.setPrice(-storeProduct.getPrice());
            }
            storeProduct.setId(UuidUtils.getUuid());
            storeProductMapper.insert(storeProduct);
        }else{
            storeProduct.setId(storeProductId);
            if( direction == App.StoreType.IN ){
                storeProductMapper.add(storeProduct);
            }else if( direction == App.StoreType.OUT ){
                storeProduct.setPrice(0L);
                storeProductMapper.sub(storeProduct);
            }else{
                throw new ServiceException("update Store direction error");
            }
        }
    }

    public Long selectStoreProduct(StoreProduct storeProduct){
        return storeProductMapper.selectStoreProduct(storeProduct);
    }

    private static void check(StoreProduct storeProduct){
        if( storeProduct.getCompanyId() == null || storeProduct.getCompanyId()<0 ){
            throw new ServiceException("company id is null");
        }
        if( storeProduct.getProductId() == null || storeProduct.getProductId()<0 ){
            throw new ServiceException("product id is null");
        }
        if( storeProduct.getStoreId() == null || storeProduct.getStoreId()<0 ){
            throw new ServiceException("Store id is null");
        }
        if( storeProduct.getUnitId() == null || storeProduct.getUnitId()<0 ){
            throw new ServiceException("unit id is null");
        }
        if( StringUtils.isEmpty(storeProduct.getCustomerId()) ){
            throw new ServiceException("customer id is null");
        }
        if( storeProduct.getNumber() == null || storeProduct.getNumber()<0 ){
            throw new ServiceException("number id is null");
        }
        if( storeProduct.getPrice() == null || storeProduct.getPrice()<0 ){
            throw new ServiceException("price id is null");
        }
    }

}
